package cn.onlineclass.common;
import cn.onlineclass.entity.OUnit;
import cn.onlineclass.service.UnitService;
import cn.onlineclass.util.StringUtil;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UnitController {
    //模糊查询
    public void unitLimit(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"),1);
        AjaxPage search = UnitService.UNIT.search(pageNo,10);
        String str = JSON.toJSONStringWithDateFormat(search,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().print(str);
    }

    //增加
    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String name =request.getParameter("name");
        String description =request.getParameter("description");
        int level = StringUtil.toInt(request.getParameter("level"));
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
        int majorid =StringUtil.toInt(request.getParameter("majorid"));
        // Date createtime =StringUtil.parse(request.getParameter("createtime"));
        // Date modifytime =StringUtil.parse(request.getParameter("modifytime"));
        OUnit unit =new OUnit(name,description,level,inuse,null,null,majorid);
        AjaxResult add = UnitService.UNIT.add(unit);
        return add;
    }

    //根据id来查找单个数据
    public void get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        UnitService unit =new UnitService();
        AjaxResult get = unit.get(id);
        String str =JSON.toJSONString(get);
        response.getWriter().print(str);
    }

    //修改
    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        int level = StringUtil.toInt(request.getParameter("level"));
        boolean inuse = StringUtil.isBlank(request.getParameter("inuse"));
        int majorid = StringUtil.toInt(request.getParameter("majorid"));
        OUnit unit =new OUnit(id,name,description,level,inuse,null,null,majorid);
        AjaxResult update = UnitService.UNIT.update(unit);
        return update;
    }

    //删除
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        OUnit unit =new OUnit();
        unit.setId(id);
        unit.setInuse(false);
        AjaxResult del = UnitService.UNIT.del(id);
        return del;
    }
}
